<?php

/**
 * Description of GroupsController
 *
 * @author Francis
 */
class GroupsController extends AppController {
    
    public $uses = array('OrganisationGroupType', 'OrganisationGroup', 'OrganisationMemberGroup', 'Member');
    
    public function beforeFilter() {
        parent::beforeFilter();
        
        $this->autoRender = false;
    }
    
    public function group($action = 'list') {
        
        $response = array();
        
        switch($action) {
            case 'list':
                $response = $this->listGroups();
                break;
            
            case 'save':
                $response = $this->saveGroup();
                break;
            
            case 'remove':
                $response = $this->removeGroup();
                break;
            
            case 'members':
                $response = $this->members();
                break;
        }
        
        return $this->_response($response);
    }
    
    private function listGroups() {
        $data = $this->request->data;
        
        // return single group
        if( isset($data['group_id']) ) {
            $group = $this->OrganisationGroup->find('first', array(
                'contain' => array(
                    'OrganisationGroupType', 
                    'OrganisationGroupLeader' => array(
                        'OrganisationMember' => array(
                            'fields' => array('id', 'organisation_no'),
                            'Member' => array(
                                'fields' => array('id', 'first_name', 'last_name', 'middle_name', 'title'),
                                'MemberImage' => array(
                                    'order' => array('MemberImage.id' => 'desc'),
                                    'limit' => 1
                                )
                            )
                        ),
                        'conditions' => array('OrganisationGroupLeader.active' => 1)
                    )
                ),
                'conditions' => array(
                    'OrganisationGroup.id' => $data['group_id'],
                    'OrganisationGroup.organisation_id' => $data['organisation_id'],
                    'OrganisationGroup.active' => 1
                )
            ));
            
            return $group;
        }
        
        // return a list of groups
        if( isset($data['type_id']) ) {
            $groups = $this->OrganisationGroup->find('all', array(
                'conditions' => array(
                    'OrganisationGroup.organisation_id' => $data['organisation_id'],
                    'OrganisationGroup.organisation_group_type_id' => $data['type_id'],
                    'OrganisationGroup.active' => 1
                ),
                'order' => array('OrganisationGroup.name'),
                'recursive' => -1
            ));

            return $groups;
        }
        
        return array('status' => 'failed', 'message' => 'No groups found for category specified');
    }
    
    /**
     * 
     * @return type
     */
    public function saveGroup() {
        $data = $this->request->data;
        
        $response = $this->OrganisationGroup->save(array(
            'id' => isset($data['group_id']) ? $data['group_id'] : null,
            'name' => $data['group_name'],
            'organisation_group_type_id' => $data['category_id'],
            'organisation_id' => $data['organisation_id']
        ));
        
        return $this->_response($response);
    }
    
    public function removeGroup() {
        $data = $this->request->data;
        
        $response = $this->OrganisationGroup->save(array(
            'id' => $data['group_id'],
            'organisation_id' => $data['organisation_id'],
            'active' => 0
        ));
        
        return $response;
    }
    
    public function members() {
        $validated = $this->_validateParams(array(
            'required' => 'organisation_id, group_id'
        ));

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($this->request->data);
        
        $group = $this->OrganisationGroup->find('first', array(
            'contain' => array('OrganisationGroupType'),
            'conditions' => array(
                'OrganisationGroup.id' => $group_id
            )
        ));
        
        $members = $this->OrganisationMemberGroup->find('all', array(
            'contain' => array(
                'OrganisationMember' => array('Member')
            ),
            'conditions' => array(
                'OrganisationMemberGroup.organisation_group_id' => $group_id,
                'OrganisationMemberGroup.organisation_id' => $organisation_id
            ),
            'limit' => isset($limit) ? $limit : 30,
            'offset' => isset($offset) ? $offset : 0
        ));
        
        
        if( !$members ) {
            return $this->_response(array('status' => 'failed', 'message' => 'No members found.'));
        }
        
        return $this->_response(array('status' => 'success', 'data' => $members, 'group' => $group)); 
    }
    
    /**
     * 
     * @deprecated since version number
     */
    public function subgroups() {
        $subgroups = $this->OrganisationGroup->find('all', array(
            'conditions' => array(
                'OrganisationGroup.organisation_id' => $_POST['organisation_id'],
                'OrganisationGroup.organisation_group_type_id' => $_POST['category_id'], 
                'OrganisationGroup.active' => 1
            ),
            'order' => array('OrganisationGroup.name'),
            'recursive' => -1
        ));
        
        return $this->_response($subgroups);
    }
    
    private function listTypes() {
        $data = $this->request->data;
        
        $groups = $this->OrganisationGroupType->find('all', array(
            'contain' => isset($data['subgroups']) ? 
                array(
                    'OrganisationGroup' => array(
                        'conditions' => array(
                            'OrganisationGroup.active' => 1,
                            'OrganisationGroup.organisation_id' => $data['organisation_id']
                        ),
                        'order' => array('OrganisationGroup.name' => 'asc')
                    )
                ) : array(),
            'conditions' => array(
                'OrganisationGroupType.organisation_id' => $data['organisation_id'],
                'OrganisationGroupType.active' => 1
            ),
            'order' => array('OrganisationGroupType.name')
        ));
        
        return $groups;
    }
    
    private function saveType() {
        $data = $this->request->data;
        
        $response = $this->OrganisationGroupType->save(array(
            'id' => isset($data['category_id']) ? $data['category_id'] : null,
            'organisation_id' => $data['organisation_id'],
            'name' => $data['category_name']
        ));
        
        return $this->_response($response);
    }
    
    private function removeType() {
        $data = $this->request->data;
        
        $response = $this->OrganisationGroupType->save(array(
            'id' => $data['category_id'],
            'organisation_id' => $data['organisation_id'],
            'active' => 0
        ));
        
        return $response;
    }
    
    public function stats() {
        
        $data = $this->request->data;
        
        $stats = $this->OrganisationGroupType->group_stats($data['organisation_id']);
        
        return $this->_response($stats);
    }
    
    public function types($action = 'list') {
        $response = array();
        
        switch($action) {
            case 'list':
                $response = $this->listTypes();
                break;
            
            case 'save':
                $response = $this->saveType();
                break;
            
            case 'remove':
                $response = $this->removeType();
                break;
        }
        
        return $this->_response($response);
    }
    
    public function leaders($action = 'list') {
        $response = array('status' => 'failed', 'message' => 'Invalid request made');
        
        switch($action) {
            case 'list':
                break;
            
            case 'save':
                $response = $this->addLeader();
                break;
            
            case 'remove':
                $response = $this->removeLeader();
                break;
        }
        
        return $this->_response($response);
    }
    
    private function addLeader() {
        $data = $this->request->data;
        
        $validated = $this->_validateParams(array(
            'required' => 'organisation_id, organisation_group_id, organisation_member_id, role'
        ));

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);
        
        return $this->OrganisationGroup->addLeader($organisation_id, $organisation_group_id, $organisation_member_id, $role);        
    }
    
    private function removeLeader() {
        $data = $this->request->data;
        
        $validated = $this->_validateParams(array(
            'required' => 'organisation_id, organisation_group_leader_id'
        ));

        if ($validated != "NO_ERROR") {
            return $validated;
        }

        extract($data);
        
        return $this->OrganisationGroup->removeLeader($organisation_id, $organisation_group_leader_id);        
    }
}
